//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "USDCurrency.h"
using namespace Cephei::QL::Currencies;
#include <gen/QL/Math/Rounding.h>
#include <gen/QL/Currency.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Currencies::CUSDCurrency::CUSDCurrency () : CCurrency(CUSDCurrency::typeid)
{
    try
    {
#ifdef HANDLE
        _phUSDCurrency = NULL;
#endif
        _ppUSDCurrency = new boost::shared_ptr<QuantLib::USDCurrency> (new QuantLib::USDCurrency ( ));
        SetCurrency (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppUSDCurrency));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Currencies::CUSDCurrency::CUSDCurrency (boost::shared_ptr<QuantLib::USDCurrency>& childNative, Object^ owner) : CCurrency(CUSDCurrency::typeid)
{
#ifdef HANDLE
	_phUSDCurrency = NULL;
#endif
	_ppUSDCurrency = &childNative;
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppUSDCurrency));
}
Cephei::QL::Currencies::CUSDCurrency::CUSDCurrency (QuantLib::USDCurrency& childNative, Object^ owner) : CCurrency(CUSDCurrency::typeid)
{
#ifdef HANDLE
	_phUSDCurrency = NULL;
#endif
	_ppUSDCurrency = new boost::shared_ptr<QuantLib::USDCurrency> (&childNative);
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppUSDCurrency));
    _USDCurrencyOwner = owner;
    _CurrencyOwner = owner;
}

Cephei::QL::Currencies::CUSDCurrency::CUSDCurrency (CUSDCurrency^ copy) : CCurrency(CUSDCurrency::typeid)
{
#ifdef HANDLE
	_phUSDCurrency = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppUSDCurrency = new boost::shared_ptr<QuantLib::USDCurrency> (copy->GetShared());
        _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppUSDCurrency));
    }
}
Cephei::QL::Currencies::CUSDCurrency::CUSDCurrency (System::Type^ t) : CCurrency(CUSDCurrency::typeid)
{
#ifdef HANDLE
	_phUSDCurrency = NULL;
#endif
	if (!t->IsSubclassOf(CUSDCurrency::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Currencies::CUSDCurrency::CUSDCurrency (QuantLib::Handle<QuantLib::USDCurrency>& childNative, Object^ owner)  : CCurrency(CUSDCurrency::typeid)
{
	_phUSDCurrency = &childNative;
	_ppUSDCurrency = &static_cast<boost::shared_ptr<QuantLib::USDCurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppUSDCurrency));
    _USDCurrencyOwner = owner;
}
Cephei::QL::Currencies::CUSDCurrency::CUSDCurrency (QuantLib::Handle<QuantLib::USDCurrency> childNative)  : CCurrency(CUSDCurrency::typeid)
{
	_phUSDCurrency = &childNative;
	_ppUSDCurrency = &static_cast<boost::shared_ptr<QuantLib::USDCurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppUSDCurrency));
}
#endif
#ifdef STRUCT
Cephei::QL::Currencies::CUSDCurrency::CUSDCurrency (QuantLib::USDCurrency childNative)  : CCurrency(CUSDCurrency::typeid)
{
#ifdef HANDLE
	_phUSDCurrency = NULL;
#endif
	_ppUSDCurrency = new boost::shared_ptr<QuantLib::USDCurrency> (new QuantLib::USDCurrency (childNative));
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppUSDCurrency));
}
#endif

Cephei::QL::Currencies::CUSDCurrency::~CUSDCurrency ()
{
    if (_ppUSDCurrency != NULL)
    {
	    delete _ppUSDCurrency;
        _ppUSDCurrency = NULL;
    }
}
Cephei::QL::Currencies::CUSDCurrency::!CUSDCurrency ()
{
    if (_ppUSDCurrency != NULL)
    {
	    delete _ppUSDCurrency;
    }
}
QuantLib::USDCurrency& Cephei::QL::Currencies::CUSDCurrency::GetReference ()
{
    if (_ppUSDCurrency == NULL) throw gcnew NativeNullException ();
	return **_ppUSDCurrency;
}
boost::shared_ptr<QuantLib::USDCurrency>& Cephei::QL::Currencies::CUSDCurrency::GetShared ()
{
    if (_ppUSDCurrency == NULL) throw gcnew NativeNullException ();
	return *_ppUSDCurrency;
}
QuantLib::USDCurrency* Cephei::QL::Currencies::CUSDCurrency::GetPointer ()
{
    if (_ppUSDCurrency == NULL) throw gcnew NativeNullException ();
	return &**_ppUSDCurrency;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::USDCurrency>& Cephei::QL::Currencies::CUSDCurrency::GetHandle ()
{
	if (_phUSDCurrency == NULL)
	{
		_phUSDCurrency = new Handle<QuantLib::USDCurrency> (*_ppUSDCurrency);
	}
	return *_phUSDCurrency;
}
#endif
bool Cephei::QL::Currencies::CUSDCurrency::HasNative () 
{
	return (_ppUSDCurrency != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Currencies::IUSDCurrency^ Cephei::QL::Currencies::CUSDCurrency_Factory::Create ()
{
    return gcnew CUSDCurrency ();
}
